package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.Obj_StuDAO;
import cn.herlioner.pojo.Obj_Stu;
import cn.herlioner.pojo.Obj_Tea;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.PageService;

public class Obj_StuDAOImpl implements Obj_StuDAO{

	@Override
	public void add(Obj_Stu os) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(os);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public Obj_Stu getbyid(Long id) {
		Obj_Stu result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Obj_Stu) s.get(Obj_Stu.class, id);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(int id) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        Obj_Stu p = (Obj_Stu) s.get(Obj_Stu.class, id);
	 
	        s.delete(p);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public void update(Obj_Stu os) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        s.update(os);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public List<Obj_Stu> list() {
		List<Obj_Stu> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Obj_Stu os");
    
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}
	


	@Override
	public List<Obj_Stu> getbystunum(Long stunum) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		List<Obj_Stu> list = s
				.createQuery("from Obj_Stu where stunum = :stunum")
				.setLong("stunum", stunum).list();
		if (list.size() == 0) {
			s.close();
			sf.close();
			return null;// 此学生没有课程安�?
		}
		s.close();
		sf.close();
		return list;
	}

	@Override
	public PageService getByPaging(int currentPage, int counta) {
       SessionFactory sf = new Configuration().configure()
               .buildSessionFactory();
       Session s = sf.openSession();
       // 查询列表 
       List result = s.createQuery("from Obj_Stu os order by os.id desc")
    		   .setFirstResult(currentPage)
    		   .setMaxResults(counta)
    		   .list();

       // 查询总记录数�? 
       Long count = (Long) s.createQuery(//  
               "SELECT COUNT(*) FROM Obj_Stu os")//  
               .uniqueResult(); 
       
       s.close();
       sf.close();
       return new PageService(currentPage, counta, count.intValue(), result);
	}

	@Override
	public List<Obj_Stu> getbyobjnum(Long objnum) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		List<Obj_Stu> list = s
				.createQuery("from Obj_Stu where objnum = :objnum")
				.setLong("objnum", objnum).list();
		if (list.size() == 0) {
			s.close();
			sf.close();
			return null;// 此学生没有课程安�?
		}
		s.close();
		sf.close();
		return list;
	}

	public PageService getByPageByStunum(int currentPagechange, int pageSize,
			Long stunum) {
		List<Obj_Stu> Obj_stulist = new ArrayList<Obj_Stu>();
		List<TSObject> TSObjectlist = new ArrayList<TSObject>();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        // 获取该学号下面所属的课程
        // 1， 获取obj_stu列表
        SQLQuery q = (SQLQuery) s.createSQLQuery("select * FROM obj_stu o where stunum = :stunum")
        		.addEntity(Obj_Stu.class)
        		.setLong("stunum", stunum)
        		.setFirstResult((currentPagechange - 1) * pageSize)
        		.setMaxResults(pageSize);
        Obj_stulist = q.list();
        // 2，根据列表内的objnum获取tsobject
        for(Obj_Stu obj_stu :Obj_stulist) 
        {	
    		TSObject result = new TSObject();
        	long objnum = obj_stu.getObjnum();
        	System.out.println(objnum);
        	result = (TSObject)s.createQuery("from TSObject o where objnum=?").setLong(0, objnum).list().get(0);
            TSObjectlist.add(result);
        }
        
        //查询总数
        Long count = (Long) s.createQuery(//  
                "SELECT COUNT(*) FROM TSObject o")//  
                .uniqueResult(); 
        
        s.close();
        sf.close();
        System.out.println(TSObjectlist);
        return new PageService(currentPagechange, pageSize, count.intValue(), TSObjectlist);
	}

	public long getMaxID() {
		
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	     Session s = sf.openSession();
	     
	     String hql = "select max(ot.id) from obj_stu as ot";
	     Query query = s.createSQLQuery(hql);
	     
	     int max = (int)query.uniqueResult();
	     
	     Long maxid = new Long((long)max);

	     
	     s.close();
	     sf.close();
	     return maxid;
	}

}
